<?php

/**
 * This is the model class for table "company_address".
 */
class company_address extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'company_address':
	 * @var integer $id
	 * @var integer $company_id
	 * @var string $address
	 * @var string $postal
	 * @var string $type
	 * @var string $avoid_time
	 * @var string $operation_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return company_address the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'company_address';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('company_id', 'numerical', 'integerOnly'=>true),
			array('postal', 'length', 'max'=>15),
			array('type', 'length', 'max'=>20),
			array('avoid_time, operation_time', 'length', 'max'=>50),
			array('company_id','exist','className'=>'company','attributeName'=>'id'),
			array('address','safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, company_id, address, postal, type, avoid_time, operation_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'contacts'=>array(self::HAS_MANY,'company_address_contacts','foreign_key','condition'=>'for_model="address"'),
			'email'=>array(self::HAS_MANY,'company_address_email','foreign_key','condition'=>'for_model="address"'),
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'company_id' => 'Company',
			'address' => 'Address',
			'postal' => 'Postal',
			'type' => 'Type',
			'avoid_time' => 'Avoid Time',
			'operation_time' => 'Operation Time',
		);
	}
	public function afterFind(){
		if($this->contacts == null)
			$this->contacts = array(new company_address_contacts);
		if($this->email == null)
			$this->email = array(new company_address_email);
		return parent::afterFind();
	}
	public function getRef($type){
		if(count($this->$type) == 0)
			return null;
		$_ref = array();
		foreach($this->$type as $ref) {
			$_ref[$ref->type][] = $ref->ref;
		}
		$__ref = array();
		foreach($_ref as $key=>$value) {
			$__ref[] = '('.$key.') '.implode(' / ',$value);
		}
		return implode(', ',$__ref);
	}
	public function AllAddress(){
		$criteria = new CDbCriteria;
		$criteria->compare('company_id',$this->company_id);
		return new CActiveDataProvider(get_class($this),array(
			'criteria'=>$criteria,
			'pagination'=>array(
       			'pageSize'=>5,
    		),
		));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('company_id',$this->company_id);

		$criteria->compare('address',$this->address,true);

		$criteria->compare('postal',$this->postal,true);

		$criteria->compare('type',$this->type,true);

		$criteria->compare('avoid_time',$this->avoid_time,true);

		$criteria->compare('operation_time',$this->operation_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}